package com.xiaochun.hxhj;


import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.provider.Settings;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.webkit.JavascriptInterface;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.core.app.ActivityCompat;
import androidx.core.content.FileProvider;

import com.myview.CustomProgressDialog;
import com.tencent.mm.sdk.modelpay.PayReq;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.tencent.smtt.export.external.interfaces.SslError;
import com.tencent.smtt.export.external.interfaces.SslErrorHandler;
import com.tencent.smtt.export.external.interfaces.WebResourceError;
import com.tencent.smtt.export.external.interfaces.WebResourceRequest;
import com.tencent.smtt.sdk.ValueCallback;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;
import com.util.DensityUtil;
import com.util.ExitAppUtil;
import com.util.InfoEventMessage;
import com.util.MyLog;
import com.util.RequestManager;
import com.util.ShareUtil;
import com.util.StatusBarUtil;
import com.util.ToastUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

//商城

public class WebviewActivity2 extends Activity implements View.OnClickListener {


    private WebView webview;
    private CustomProgressDialog customProgressDialog;
    private RelativeLayout errorview;
    private boolean loadError = false;
    private IWXAPI api;
    private WebSettings webSettings;
    private String shopurl;
    private String paymessage = "";
    private Dialog downloadDialog;
    private Thread downLoadThread;
    private String apkurl = "";

    private static final int DOWN_UPDATE = 1;

    private static final int DOWN_OVER = 2;
    private static final int DOWN_ERROR = 4;
    private int progress;
    private ProgressBar mProgress;
    private Dialog noticeDialog;
    private Uri photoUri;
    private Uri photoUricat;
    private String UppicPath;

    private File imgFile;
    private boolean is_shot;//是否来源于拍照
    private String userlogoupload;
    private String title;
    private String desc;
    private LinearLayout ll_rebackw;
    private TextView tv_titlew;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview2);
        MyLog.e("网页网特", "网页网特22222");
        StatusBarUtil.transparencyBar(this); //设置状态栏全透明
        StatusBarUtil.StatusBarLightMode(this); //设置白底黑字
        customProgressDialog = new CustomProgressDialog(WebviewActivity2.this, "", R.style.CommProgressDialog);
        WindowManager.LayoutParams params = customProgressDialog.getWindow().getAttributes();
        params.alpha = 0.7f;//取值为0-1之间  0是全透明，1是不透明
        params.dimAmount = 0f;   //取值为0-1之间  1是全黑
//		params.width = 180;//设置进度条的宽度
//		params.height = 180;//设置进度条的宽度
        customProgressDialog.getWindow().setAttributes(params);
        customProgressDialog.show();
        getWindow().addFlags(
                WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
        initView();
    }


    protected void initView() {

        ll_rebackw = findViewById(R.id.ll_reback);

        ll_rebackw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        tv_titlew = findViewById(R.id.tv_title);

        webview = findViewById(R.id.webview);
        String type = getIntent().getStringExtra("type");
        tv_titlew.setText(type);
        shopurl = getIntent().getStringExtra("url");
//        shopurl = shopurl+"&token="+token;
//        String shopurl = getUserInfo("shopurl");
        errorview = findViewById(R.id.online_error_btn_retry);

        //支持缩放，默认为true。
        webSettings = webview.getSettings();
        webSettings.setUseWideViewPort(true); // 缩放至屏幕的大小 webSettings
        webSettings.setLoadWithOverviewMode(true); //设置默认编码
        webSettings.setDefaultTextEncodingName("utf-8"); ////设置自动加载图片
        webSettings.setLoadsImagesAutomatically(true);
        webSettings.setJavaScriptEnabled(true);// 设置可以运行JS脚本
        webSettings.setSupportZoom(false);// 用于设置webview放大
        webSettings.setBuiltInZoomControls(false);


        webSettings.setDomStorageEnabled(true);


        //支持js
        webSettings.setJavaScriptEnabled(true);
        //设置本地调用对象及其接口
        webview.addJavascriptInterface(new Waimairenjsobj(), "h5_android");
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                //在这里执行你想调用的js函数
                if (loadError != true) {
                    errorview.setVisibility(View.GONE);
                    webview.setVisibility(View.VISIBLE);
                }


            }


            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
                super.onReceivedSslError(view, handler, error);
            }

            @Override
            public void onReceivedError(WebView webView, WebResourceRequest webResourceRequest, WebResourceError webResourceError) {
                super.onReceivedError(webView, webResourceRequest, webResourceError);
                if (customProgressDialog != null) {
                    if (customProgressDialog.isShowing()) {
                        customProgressDialog.dismiss();
                    }
                }
                webView.setVisibility(View.GONE);
                errorview.setVisibility(View.VISIBLE);
                loadError = true;
            }

            @Override
            public void onReceivedError(WebView webView, int i, String s, String s1) {
                super.onReceivedError(webView, i, s, s1);
                //这里进行无网络或错误处理，具体可以根据errorCode的值进行判断，做跟详细的处理。
//                hideErrorPage();
//                showErrorPage();
                if (customProgressDialog != null) {
                    if (customProgressDialog.isShowing()) {
                        customProgressDialog.dismiss();
                    }
                }
                webView.setVisibility(View.GONE);
                errorview.setVisibility(View.VISIBLE);
                loadError = true;

            }
        });
//        webview.setLayerType(View.LAYER_TYPE_HARDWARE, null);


        //3、 加载需要显示的网页
        webview.loadUrl(shopurl);
        webview.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress == 100) {
                    if (customProgressDialog != null) {
                        if (customProgressDialog.isShowing()) {
                            customProgressDialog.dismiss();
                        }
                    }
                }
            }
        });

    }


    @Override
    protected void onPause() {
        webview.reload();

        super.onPause();
    }

    @Override
    public void onClick(View view) {

    }

    private final class Waimairenjsobj {
        // 打电话
        @JavascriptInterface
        public void reback() {
            finish();
        }



    }



}
